package com.example.yunJie.digitalSentry.control;


import com.example.yunJie.digitalSentry.entity.SchedulingEntity;
import com.example.yunJie.digitalSentry.entity.ShiftEntity;
import com.example.yunJie.digitalSentry.service.PersonalService;
import com.example.yunJie.digitalSentry.service.SchedulingService;
import com.example.yunJie.digitalSentry.service.ShiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/Scheduling")
public class SchedulingControl {
    @Autowired
    private ShiftService shiftService;

    @Autowired
    private PersonalService personalService;

    @Autowired
    private SchedulingService schedulingService;

    @PostMapping("/add")
    public void insert(@RequestBody List<SchedulingEntity> list) {
        for (SchedulingEntity schedulingEntity : list) {
            System.out.println(schedulingEntity);
            schedulingEntity.setShiftId(String.valueOf(shiftService.findBy(schedulingEntity.getMarketName(), schedulingEntity.getPostName(), schedulingEntity.getShiftName()).getId()));
            schedulingEntity.setPersonalId(String.valueOf(personalService.findByPersonalName(schedulingEntity.getPersonalName()).getId()));
            if (schedulingService.findByPersonalIdAndShiftId(schedulingEntity) == null) {
                schedulingService.insert(schedulingEntity);
            }
        }
    }

    @RequestMapping("/update")
    public int update(SchedulingEntity schedulingEntity) {
        schedulingEntity.setShiftId(String.valueOf(shiftService.findByShiftName(schedulingEntity.getShiftName()).getId()));
        schedulingEntity.setPersonalId(personalService.findByPersonalName(schedulingEntity.getPersonalName()).getPersonalId());
        return schedulingService.update(schedulingEntity);
    }

    @RequestMapping("/deleteById")
    public int deleteById(int id) {
        return schedulingService.deleteById(id);
    }

    @RequestMapping("/deleteIdList")
    public int deleteIdList(Long[] id) {
        return schedulingService.deleteIdList(id);
    }

    @RequestMapping("/findId")
    public SchedulingEntity findId(Serializable id) {
        return schedulingService.findId(id);
    }

    @RequestMapping("/findAll")
    public List<SchedulingEntity> findAll(SchedulingEntity schedulingEntity) {
        System.out.println(schedulingEntity);
        return schedulingService.findAll(schedulingEntity);
    }

    //根据市场名称，查找所有的排班列表
    @RequestMapping("/findAllByMarketName")
    public List<SchedulingEntity> findAllByMarketName(String marketName) {
        return schedulingService.findAllByMarketName(marketName);
    }

    //根据市场名称和岗位名称查找排班列表
    @RequestMapping("/findAllByMarketNameAndPostName")
    public List<SchedulingEntity> findAllByMarketNameAndPostName(String marketName, String postName) {
        return schedulingService.findAllByMarketNameAndPostName(marketName, postName);
    }

    //根据市场名，岗位名，班次名查找排班列表
    @RequestMapping("/findAllByMarketNameAndPostNameAndShiftName")
    public List<SchedulingEntity> findAllByMarketNameAndPostNameAndShiftName(String marketName, String postName, String shiftName) {
        return schedulingService.findAllByMarketNameAndPostNameAndShiftName(marketName, postName, shiftName);
    }


}
